/**
 * Created by Administrator on 2018/6/8.
 */
function personalInfoInit1($scope, $http, certusUtils1, $routeParams, CertusFormAjax) {

    $scope.close = function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    $scope.mouseoverDept = function (aa) {
        certusUtils1.ajax("querySuperiorDepartment", {deptCode: aa}, function (res) {
            $scope.show = true;
            $scope.superiorDeptName = res.data;
        })
    }

    $scope.userOperate = function () {
        //console.log($scope.sdata);
        // $.post('/userOperate',JSON.stringify($scope.baseUser),function(d){
        //     console.log(d);
        // })
        if (certusValidateNotnull()) {
            var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z\.]{2,10}$/;
            if (!myreg.test($scope.baseUser.email)) {
                layer.msg("邮箱格式不正确 请检查")
                return false;
            }
            if(!$scope.baseUser.leaderId){
                layer.msg("请填写正确上级领导")
                return;
            }
            CertusFormAjax.ajax("userOperateNoAuthority", $scope.baseUser, function (response) {


                $scope.baseUser.leaderId = $("#leaderNameId").val();
                $scope.baseUser.leaderName = $("#leaderNameId").val();

                if ("success" == response.data) {
                    // var index=parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);
                    history.back();
                } else if ("logout" == response.data) {
                    certusUtils1.logout();
                } else if ("error" == response.data) {
                    layer.msg("保存失败");
                    history.back();
                }
            })
        }

    };

    var picker2 = $('#datetime').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    }).on('dp.change', function (e) {
        var result = new moment(e.date).format('YYYY-MM-DD');
        if (result && !angular.isUndefined(result)) {
            $scope.baseUser.birthday = result;
            $scope.$apply();
        }
    })

    var picker3 = $('#datetime1').datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    }).on('dp.change', function (e) {
        var result = new moment(e.date).format('YYYY-MM-DD');
        if (result && !angular.isUndefined(result)) {
            $scope.baseUser.enrollTime = result;
            $scope.$apply();
        }
    })

    var param = {};
    url = "editPersonInfo";
    param = {
        "userId": $routeParams.userId
    }
    $scope.personDisabled = {};
    $scope.personDisabled.ngper = true;


    certusUtils1.ajax(url, param, function (result) {
            var baseUser = eval("(" + result.data + ")");
            //用于 初始化 select
            // if(baseUser.departmentUser.departmentCode){
            //     var departmentCode = baseUser.departmentUser.departmentCode;
            // }

            //disabled 初始化
            for (var i in baseUser) {

                $scope.personDisabled[i] = true;
            }
            //生日、电话号码、常住地、企业微信号、直接领导、姓名、性别、岗位、婚姻，邮箱，企业微信号
            $scope.personDisabled['birthday'] = false;
            $scope.personDisabled['phoneNumber'] = false;
            $scope.personDisabled['basedArea'] = false;
            $scope.personDisabled['weChatId'] = false;
            $scope.personDisabled['userName'] = false;
            $scope.personDisabled['sex'] = false;
            $scope.personDisabled['positonName'] = false;
            $scope.personDisabled['isMarriage'] = false;
            $scope.personDisabled['email'] = false;
            $scope.personDisabled['leaderName'] = false;

        CertusFormAjax.ajax("findSpUserList3",{},function(res){
            if(res.data) {
                res.data.forEach(function (data) {
                    $("#leaderNameId").append('<option  value="' + data.userId + '">' + data.userName + "(" + data.positonName + ")" + '</option>')
                })
                $('#leaderNameId').on('changed.bs.select', function (e) {

                    if (e.target.value) {
                        $scope.baseUser.leaderId = e.target.value;
                        $scope.baseUser.leaderName = e.target.selectedOptions[0].innerHTML;
                    }
                })
                $("#leaderNameId").selectpicker('refresh')
                $("#leaderNameId").selectpicker('val',$scope.baseUser.leaderId);

            }
        })

            function isSamePinyin1(element) {
                if (userPinin) {
                    userPinin = userPinin.toUpperCase();
                }
                var pinyin = "";
                var userName2 = "";
                var userName = "";
                if (element.pinyin) {
                    pinyin = element.pinyin.toUpperCase();
                }
                if (element.userName2) {
                    userName2 = element.userName2.toUpperCase();
                }
                if (element.userName) {
                    userName = element.userName.toUpperCase();
                }

                //部门名称 联动 查询
                return pinyin.indexOf(userPinin) > -1 || userName2.indexOf(userPinin) > -1 || userName.indexOf(userPinin) > -1;
            }
            var userPinin;
            //领导者 姓名变化

            var sex = baseUser.sex;
            var isMarriage = baseUser.isMarriage;
            var onPost = baseUser.onPost;

            $scope.baseUser = baseUser

            //性别 选择框
            //初始化 select 选择框 数据
            $scope.sexSelect = [
                {sexName: '男', sex: "1"},
                {sexName: '女', sex: "0"}
            ];

            $scope.baseUser.sex = sex + "";

            $scope.marriageSelect = [
                {marriageName: "未婚", isMarriage: "0"},
                {marriageName: "已婚", isMarriage: "1"},
                {marriageName: "已婚已孕", isMarriage: "2"}
            ]

            $scope.onPostSelect = [
                {onPost: "1", onPostName: "在职"},
                {onPost: "0", onPostName: "离职"}
            ]

            $scope.baseUser.isMarriage = isMarriage + "";

            $scope.baseUser.onPost = onPost + "";
            CertusFormAjax.ajax("companyList", {}, function (res) {
                $scope.companySelect = eval("(" + res.data + ")");
                certusUtils1.ajax("deptedList", {"companyCode": $scope.baseUser.departmentUser.companyCode}, function (result) {
                        //初始化 公司 select 选择框 数据
                        $scope.departmentSelect = eval("(" + result.data + ")");

                        for (var i in $scope.departmentSelect) {
                            if ($scope.departmentSelect[i].departmentCode == $scope.baseUser.departmentUser.departmentCode) {
                                $scope.departmentName = $scope.departmentSelect[i].departmentName;
                            }
                        }
                        // $scope.baseUser.departmentUser = {};
                        //初始化select 选择框
                        // $scope.baseUser.departmentUser.departmentCode = departmentCode;
                        // $scope.baseUser.birthday = $scope.baseUser.birthday.substring(0,7);
                    }
                )
            })
            // $scope.baseUser.birthday = $scope.baseUser.birthday.substring(0,7);
        }
    )

    $scope.changeDept = function (a, b, c) {
        certusUtils1.ajax("deptedList", {companyCode: a}, function (res) {
            $scope.departmentSelect = eval("(" + res.data + ")");
            //先判断 是否 有 参数值 带过来

        })
    }


    //只保留 数字
    $scope.clearNum = function (obj, attr) {
        $scope.baseUser[attr] = $scope.baseUser[attr].replace(/\D/g, "");
    }


}